﻿#include "rec_schedule.h"

//public
CSchedule::CSchedule()
{
	memset(&m_schedule, 0, sizeof (SCHDULE_REC));
	for(int i = 0 ; i < 7; i++)
	{
		m_schedule.scheduleTime[i] = (uint64_t)0;
	}
}

CSchedule::CSchedule(const SCHDULE_REC &schedule)
{
	memcpy(&m_schedule, &schedule, sizeof(SCHDULE_REC));
}

CSchedule::~CSchedule()
{

}

bool CSchedule::GetWeekdaySchedule(uint16_t wDay, uint16_t hour, uint16_t min)
{
#if 0
	for (unsigned uint16_t i=0; i<MAX_DATE_PERIOD_NUM; ++i)
	{
		if (m_schedule.wDay[wDay][i].bEnable)
		{
			if (hour < m_schedule.wDay[wDay][i].startHour)
			{
				continue;
			}

			if ((hour == m_schedule.wDay[wDay][i].startHour) && (min < m_schedule.wDay[wDay][i].startMinute))
			{
				continue;
			}

			if ((m_schedule.wDay[wDay][i].endHour == hour) && (m_schedule.wDay[wDay][i].endMinute < min))
			{
				continue;
			}

			if (m_schedule.wDay[wDay][i].endHour < hour)
			{
				continue;
			}

			return true;
		}
	}
#endif
	return false;
}

bool CSchedule::GetDefaultSchedule(const int64_t & time)
{
#if 0
	for (unsigned uint16_t i=0; i<TVT_MAX_DEFINED_PERIOD_NUM; ++i)
	{
		if (m_schedule.defined[i].bEnable)
		{
			int64_t endTime;
			int64_t startTime;
			startTime = m_schedule.defined[i].startTime;
			endTime = m_schedule.defined[i].endTime;

			if ((startTime <= time) && (time <= endTime))
			{
				return true;
			}
		}
	}
#endif
	return false;
}
//end

bool CSchedule::IsEffectTime(const time_t timeSecs)
{
	struct tm *tm_time;

	tm_time = gmtime( &timeSecs);

	long long base = 1;

	if( tm_time->tm_wday == 0 )
	{
		tm_time->tm_wday = 7;
	}

	int nTimeBlock = ((tm_time->tm_hour) *2+tm_time->tm_min/30);
	if(m_schedule.scheduleTime[tm_time->tm_wday-1] & (base<<nTimeBlock) )
	{
		//TRACE("%d day Time is marked.\n",tm_time->tm_wday);
		return true;
	}
	else
	{
		return false;
	}

	return false;
}

//void DecMinute(unsigned char & h, unsigned char & m)
//{
//	if (0 == m)
//	{
//		assert (h > 0);
//		-- h;
//		m = 59;
//	}
//	else
//	{
//		-- m;
//	}
//}
//
//void CSchedule::ScheduleToTimeSec(std::list<TIME_SEC> & timeSecList, const TVT_DATE_SCHEDULE &date)
//{
//	//先清空列表
//	timeSecList.clear();
//
//	TIME_SEC sec;
//	memset(&sec, 0, sizeof(TIME_SEC));
//
//	bool bStart = false;
//	for (unsigned char h=0; h<24; ++h)
//	{
//		for (unsigned char m=0; m<60; ++m)
//		{
//			//发现m对应的分钟为有效，且此时需要寻找一段的开始
//			if ((date.hour[h].minute & ((ULONGLONG)(0x01) << m)) && !bStart)
//			{
//				sec.startTimeHour	= h;
//				sec.startTimeMinute	= m;
//				bStart = !bStart;
//			}
//
//			//发现m对应的分钟为无效，且此时需要寻找一段的结束
//			if ((0 == (date.hour[h].minute & ((ULONGLONG)(0x01) << m))) && bStart)
//			{
//				sec.endTimeHour		= h;
//				sec.endTimeMinute	= m;
//
//				DecMinute(sec.endTimeHour, sec.endTimeMinute);
//
//				bStart = !bStart;
//
//				timeSecList.push_back(sec);
//			}
//		}
//	}
//
//	//最后一分钟也是有效的，所以只有开始标记，而没有找到结束标记
//	if (bStart)
//	{
//		sec.endTimeHour		= 23;
//		sec.endTimeMinute	= 59;
//
//		timeSecList.push_back(sec);
//	}
//}
//
//void CSchedule::TimeSecToSchedule(const std::list<TIME_SEC> & timeSecList, TVT_DATE_SCHEDULE &date, const std::list<TIME_SEC>* pTmSecListDec)
//{
//	memset(&date, 0, sizeof(TVT_DATE_SCHEDULE));
//
//	if (0 != timeSecList.size())
//	{
//		std::list<TIME_SEC>::const_iterator it;
//		for (it = timeSecList.begin(); it != timeSecList.end(); ++it)
//		{
//			const TIME_SEC & sec = *(it);
//
//			{
//				//一个小时之内
//				if (sec.startTimeHour == sec.endTimeHour)
//				{
//					for (unsigned char m=sec.startTimeMinute; m<=sec.endTimeMinute; ++m)
//					{
//						date.hour[sec.startTimeHour].minute |= ((ULONGLONG)(0x01) << m);
//					}
//				}
//				else if (sec.startTimeHour < sec.endTimeHour)
//				{
//					//开始一个小时内部
//					ULONGLONG info = 0;
//					for (unsigned char m=sec.startTimeMinute; m<60; ++m)
//					{
//						info |= ((ULONGLONG)(0x01) << m);
//					}
//					date.hour[sec.startTimeHour].minute |= info;
//
//					for (unsigned char h=sec.startTimeHour+1; h<sec.endTimeHour; ++h)
//					{
//						date.hour[h].minute = ((ULONGLONG)0x0fffffff<<32) + (ULONGLONG)(0xffffffff);
//					}
//
//					info = 0;
//					for (unsigned char m=0; m<=sec.endTimeMinute; ++m)
//					{
//						info |= ((ULONGLONG)(0x01) << m);
//					}
//					date.hour[sec.endTimeHour].minute |= info;
//				}
//			}
//
//			//	it = timeSecList.erase(it);
//		}
//	}
//
//	if(pTmSecListDec && 0 != pTmSecListDec->size())
//	{
//		std::list<TIME_SEC>::const_iterator it;
//		for (it = pTmSecListDec->begin(); it != pTmSecListDec->end(); ++it)
//		{
//			const TIME_SEC & sec = *(it);
//
//			{
//				//一个小时之内
//				if (sec.startTimeHour == sec.endTimeHour)
//				{
//					for (unsigned char m=sec.startTimeMinute; m<=sec.endTimeMinute; ++m)
//					{
//						date.hour[sec.startTimeHour].minute &= ~((ULONGLONG)(0x01) << m);
//					}
//				}
//				else if (sec.startTimeHour < sec.endTimeHour)
//				{
//					//开始一个小时内部
//					ULONGLONG info = 0;
//					for (unsigned char m=sec.startTimeMinute; m<60; ++m)
//					{
//						info |= ((ULONGLONG)(0x01) << m);
//					}
//					date.hour[sec.startTimeHour].minute &= ~info;
//
//					for (unsigned char h=sec.startTimeHour+1; h<sec.endTimeHour; ++h)
//					{
//						date.hour[h].minute = 0/*((ULONGLONG)0x0fffffff<<32) + (ULONGLONG)(0xffffffff)*/;
//					}
//
//					info = 0;
//					for (unsigned char m=0; m<=sec.endTimeMinute; ++m)
//					{
//						info |= ((ULONGLONG)(0x01) << m);
//					}
//					date.hour[sec.endTimeHour].minute &= ~info;
//				}
//			}
//		}
//	}
//}
